//
//  HZCommonTool.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/15.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZCommonTool.h"
#import "config.h"

@implementation HZCommonTool

+ (void)toolChangeScreenOrientation:(UIInterfaceOrientation)aUIInterfaceOrientation
                 withViewController:(UIViewController *)viewController
{
    BOOL isHidden = YES;
    if (aUIInterfaceOrientation == UIDeviceOrientationPortrait) {
        isHidden = NO;
    }
    [[UIApplication sharedApplication] setStatusBarOrientation: aUIInterfaceOrientation animated:YES];
    [viewController.navigationController setNavigationBarHidden:isHidden animated:NO];//必须置于设置状态栏方向后面
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    //    self.view.transform = CGAffineTransformIdentity;
    UIInterfaceOrientation orientation = aUIInterfaceOrientation;
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    CGRect frame = [UIScreen mainScreen].bounds;
    CGPoint center = CGPointMake(frame.size.width/2, frame.size.height/2);
    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        transform = CGAffineTransformMakeRotation(M_PI*1.5);
    }else if (orientation == UIInterfaceOrientationLandscapeRight) {
        transform = CGAffineTransformMakeRotation(M_PI/2);
    }else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
        transform = CGAffineTransformMakeRotation(-M_PI);
    }else {
        transform = CGAffineTransformIdentity;
    }
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:duration];
    viewController.view.transform = transform;
    viewController.view.center = center;
    [UIView commitAnimations];
    [viewController.view layoutIfNeeded];
    [viewController.view setNeedsLayout];

}

+ (BOOL)isShowNormal
{
    BOOL isShow = false;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [MYCommonTool getFilePathWithFileName:@"verConfig.plist"];
    if (![fileManager fileExistsAtPath:filePath]) {
        NSString *pBundlePath = [[NSBundle mainBundle] pathForResource:@"verConfig.plist" ofType:nil];
        NSError *error;
        BOOL isSuccess = [fileManager copyItemAtPath:pBundlePath toPath:filePath error:&error];
        if (!isSuccess) {
            DLog(@"拷贝内容失败%@",error);
            return false;
        }
    }
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    if ([dict.allKeys containsObject:@"isShowNormal"]) {
        isShow = [dict[@"isShowNormal"] boolValue];
    }
    
    return isShow;
}
+ (void)writeShow:(BOOL)isShow
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [MYCommonTool getFilePathWithFileName:@"verConfig.plist"];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSDictionary *dict = @{@"isShowNormal": @(isShow)};
        [dict writeToFile:filePath atomically:YES];
    }
}

+ (NSInteger)toolThemeType
{
    return atoi(GetConfig("Theme"));
}

+ (void)toolUpdateThemeType:(NSString *)type
{
    AddConfig("Theme", (char *)type.UTF8String);
    SaveConfig();
}

//颜色主题

+ (UIColor *)getThemeColor
{
    return [self getThemeColorWithDefaultColor:nil];
}
+ (UIColor *)getThemeColorWithDefaultColor:(UIColor *)defaultColor
{
    UIColor *pColor = [UIColor whiteColor];
    if (defaultColor) {
        pColor = defaultColor;
    }
    NSString *theme = [NSString stringWithUTF8String:GetConfig("Theme")];
    if ([theme isEqual:@"1"]) {//夜间模式
        pColor = kMYHexToRGB(0x2d2e40, 1);
    }
    return pColor;
}
+ (UIColor *)getThemeSeperateColorWithDefaultColor:(UIColor *)defaultColor
{
    UIColor *pColor = kMYHexToRGB(0xdedede, 1);
    if (defaultColor) {
        pColor = defaultColor;
    }
//    NSString *theme = [NSString stringWithUTF8String:GetConfig("Theme")];
//    if ([theme isEqual:@"1"]) {//分割线夜间模式
//        pColor = kMYHexToRGB(0x3d404f, 1);
//    }
    return pColor;
}
+ (UIColor *)getThemeBlockColorWithDefaultColor:(UIColor *)defaultColor
{
    UIColor *pColor = kMYHexToRGB(0xf5f5f5, 1);
    if (defaultColor) {
        pColor = defaultColor;
    }
    NSString *theme = [NSString stringWithUTF8String:GetConfig("Theme")];
    if ([theme isEqual:@"1"]) {//块区夜间模式
        pColor = kMYHexToRGB(0x242431, 1);
    }
    return pColor;
}

+ (UIColor *)getThemeZhangDieColor
{
    UIColor *pColor = kMYHexToRGB(0x2a2a2a, 1);
    NSString *theme = [NSString stringWithUTF8String:GetConfig("Theme")];
    if ([theme isEqual:@"1"]) {//涨跌夜间模式
        pColor = kMYHexToRGB(0xffffff, 1);
    }
    return pColor;
}
+ (UIColor *)getThemeZhangDieColorWithValue1:(CGFloat)value1 value2:(CGFloat)value2
{
    return [self getThemeZhangDieColorWithValue:value1 - value2];
}
+ (UIColor *)getThemeZhangDieColorWithValue:(CGFloat)value
{
    UIColor *pColor = kMYHexToRGB(0x2a2a2a, 1);
    NSString *theme = [NSString stringWithUTF8String:GetConfig("Theme")];
    if ([theme isEqual:@"1"]) {//涨跌夜间模式
        pColor = kMYHexToRGB(0xffffff, 1);
    }
    if (value > 0) {
        pColor = kMYHexToRGB(0xf60300,1);//红色
    }else if (value < 0) {
        pColor = kMYHexToRGB(0x30ae0e,1);//绿色
    }
    
    return pColor;
}
+ (int)getThemeMinKlineSeprateColor
{
    int nColor = 0xa7a7a7;
    NSString *theme = [NSString stringWithUTF8String:GetConfig("Theme")];
    if ([theme isEqual:@"1"]) {//夜间模式
        nColor = 0x3d404f;
    }
    return nColor;
}
+ (UIColor *)getThemeVersionFontColor
{
    UIColor *pColor = [UIColor blackColor];
    NSString *theme = [NSString stringWithUTF8String:GetConfig("Theme")];
    if ([theme isEqual:@"1"]) {//涨跌夜间模式
        pColor = [UIColor whiteColor];
    }
    return pColor;
}

@end
